package com.example.takeaway.service.impl;

import com.example.takeaway.constant.OrderStatusEnum;
import com.example.takeaway.mapper.DishMapper;
import com.example.takeaway.mapper.OrderMapper;
import com.example.takeaway.model.vo.DashboardVO;
import com.example.takeaway.model.vo.OrderDashboardVO;
import com.example.takeaway.service.DashboardService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 邢晨旭
 * {@code @date} 2023/10/26
 */
@Service
public class DashboardServiceImpl implements DashboardService {

    @Resource
    OrderMapper orderMapper;

    @Resource
    DishMapper dishMapper;

    @Override
    public DashboardVO getDashboard(Long shopId) {
        List<OrderDashboardVO> orderDashboards = orderMapper.queryOrderDashboard(shopId);
        DashboardVO dashboardVO = new DashboardVO();
        if (orderDashboards != null && orderDashboards.size() > 0) {
            long count = orderDashboards.stream().map(OrderDashboardVO::getCount).reduce(0, Integer::sum);
            Map<Integer, OrderDashboardVO> dashboardMap = orderDashboards.stream().collect(Collectors.toMap(OrderDashboardVO::getStatus, item -> item));
            //待接单
            OrderDashboardVO waitOrder = dashboardMap.get(OrderStatusEnum.WAITING.getCode());
            //待派送
            OrderDashboardVO waitDelivery = dashboardMap.get(OrderStatusEnum.PREPARING.getCode());
            //有效订单(已完成)
            OrderDashboardVO validOrder = dashboardMap.get(OrderStatusEnum.FINISHED.getCode());
            //已取消
            OrderDashboardVO cancelOrder = dashboardMap.get(OrderStatusEnum.CANCELED.getCode());
            dashboardVO.setOrderCount((int) count);
            if (validOrder != null) {
                dashboardVO.setTurnover(validOrder.getAmount());
                dashboardVO.setValidOrderCount(validOrder.getCount());
                dashboardVO.setFinishCount(validOrder.getCount());
                dashboardVO.setOrderFinishRate(BigDecimal.valueOf(validOrder.getCount()).divide(BigDecimal.valueOf(dashboardVO.getOrderCount())));
                dashboardVO.setAveragePrice(dashboardVO.getTurnover().divide(BigDecimal.valueOf(dashboardVO.getValidOrderCount())));
            }
            if (waitOrder != null) {
                dashboardVO.setWaitOrderCount(waitOrder.getCount());
            }
            if (waitDelivery != null) {
                dashboardVO.setWaitDeliveryCount(waitDelivery.getCount());
            }
            if (cancelOrder != null) {
                dashboardVO.setCancelCount(cancelOrder.getCount());
            }

            List<OrderDashboardVO> dishDashboards = dishMapper.queryDashboard(shopId);
            if (dishDashboards != null && dishDashboards.size() > 0) {
                Map<Integer, OrderDashboardVO> dishDashboardMap = dishDashboards.stream().collect(Collectors.toMap(OrderDashboardVO::getStatus, item -> item));
                if (dishDashboardMap.get(1) != null) {
                    dashboardVO.setEnableDishCount(dishDashboardMap.get(1).getCount());
                }
                if (dishDashboardMap.get(0) != null) {
                    dashboardVO.setDisableDishCount(dishDashboardMap.get(0).getCount());
                }
            }
        }
        return dashboardVO;
    }
}
